// pages/approve/approve.js
var app = getApp();
var com = require('../../common.js');
Page({
  data: {
    storeInfo: {},
    place: [],
    one: 0,
    value: [0, 0, 0],
    business: '',
    idcode_positive: "",
    idcode_other: "",
    medical: "",
    zhi: '请选择所在地区',
    markers: [{
      id: 1,
      latitude: '',
      longitude: '',
      name: '',
      width: 30,
      height: 30,
      iconPath: '../images/adres.png'
    }],
    is_show: true,
    next: 'next',
    statusd: 1,
    showLocaltion: false
  },
  golocaltoon() {
    wx.getSetting({
      success(res) {
        console.log(res)
        if (res.authSetting["scope.userLocation"] == true) {
          console.log('授权成功')
        } else {
          wx.openSetting({

          })
        }
      }
    })
  },
  onLoad: function(options) {
    wx.setStorageSync('functionId', 1)
    console.log(options)
    var _this = this,
      markers = _this.data.markers
    _this.setData({
      buttonId: options.id
    })
    wx.getLocation({
      type: "gcj02",
      success: (res) => {
        console.log('成功：', res)
        markers[0].latitude = res.latitude
        markers[0].longitude = res.longitude
        _this.setData({
          markers: markers
        })
      },
      fail: (res) => {
        _this.setData({
          showLocaltion: true
        })
        console.log('失败：', res)
      },
    })
    _this.onStoreCentent()
    _this.onRegions()
    setTimeout(() => {
      _this.setData({
        statusd: 0
      })
    }, 1500)
  },
  //获取资质认证
  onStoreCentent() {
    var _this = this,
      url = app.link.appraisalStore,
      data = {},
      t = {}
    com.link(url, data, function(rep) {
      console.log(rep)
      var i = rep.data.response.response
      t.address = i.address ? i.address : ''
      t.city = i.city ? i.city : ''
      t.district = i.district ? i.district : ''
      t.idcode = i.idcode ? i.idcode : ''
      t.business_license = i.business_license ? i.business_license : ''
      t.person = i.person ? i.person : ''
      t.province = i.province ? i.province : ''
      t.company = i.company ? i.company : ''
      t.idcode_positive = i.idcode_positive ? i.idcode_positive : ''
      t.idcode_other = i.idcode_other ? i.idcode_other : ''
      t.medical = i.medical ? i.medical : ''
      t.detailed_address = i.detailed_address ? i.detailed_address : ''
      t.license = i.license ? i.license : ''
      t.share = i.share ? i.share : ''
      t.latitude = i.latitude ? i.latitude : ''
      t.longitude = i.longitude ? i.longitude : ''
      _this.setData({
        storeInfo: t,
        zhi: i.region == '--' ? '请选择所在地区' : i.region,
        business: i.business_license_url,
        idcode_positive: i.idcode_positive_url,
        idcode_other: i.idcode_other_url,
        medical: i.medical_url,
        is_show: i.person == '' ? true : false
      })
      console.log(_this.data)
    }, 'GET')
  },
  //信息存入
  onBindInput(e) {
    console.log(e)
    var _this = this,
      i = e.currentTarget.id,
      l = _this.data.storeInfo,
      t = e.detail.value
    if (i == 1) {
      l.company = t
      _this.setData({
        storeInfo: l
      })
    } else if (i == 2) {
      var _this = this,
        markers = _this.data.markers
      wx.chooseLocation({
        success: function(res) {
          console.log(res)
          l.address = res.name
          // l.detailed_address = res.address
          l.latitude = res.latitude
          l.longitude = res.longitude
          markers[0].latitude = res.latitude
          markers[0].longitude = res.longitude
          _this.setData({
            storeInfo: l,
            markers: markers
          })
          console.log(_this.data.storeInfo)
        },
      })
    } else if (i == 3) {
      l.person = t
      _this.setData({
        storeInfo: l
      })
    } else if (i == 4) {
      l.idcode = t
      _this.setData({
        storeInfo: l
      })
    } else if (i == 5) {
      l.detailed_address = t
      _this.setData({
        storeInfo: l
      })
    } else if (i == 6) {
      l.license = t
      _this.setData({
        storeInfo: l
      })
    } else {
      l.share = t
      _this.setData({
        storeInfo: l
      })
    }
  },
  //获取省市县
  onRegions() {
    var _this = this
    com.link(app.link.regions, {}, function(rep) {
      console.log(rep)
      _this.setData({
        province: rep.data.data
      })
      com.link(app.link.region + 3 + '.st', {}, function(rep) {
        console.log(rep)
        _this.setData({
          city: rep.data.data
        })
        com.link(app.link.region + 36 + '.st', {}, function(rep) {
          console.log(rep)
          _this.setData({
            district: rep.data.data,
            place: [_this.data.province, _this.data.city, rep.data.data]
          })
        }, 'GET')
      }, 'GET')
    }, 'GET')
  },

  //获取选中的省市县
  place11: function(e) {
    console.log(e)
    var that = this;
    if (e.detail.column == 0) {
      com.link(app.link.region + that.data.province[e.detail.value].region_id + '.st', {}, function(rep) {
        that.setData({
          city: rep.data.data
        });
        com.link(app.link.region + that.data.city[0].region_id + '.st', {}, function(rep) {
          console.log(rep)
          that.setData({
            district: rep.data.data,
            place: [that.data.province, that.data.city, rep.data.data],
            one: e.detail.value,
            value: [e.detail.value, 0, 0]
          });
        }, 'GET')
      }, 'GET')
    } else if (e.detail.column == 1) {
      com.link(app.link.region + that.data.city[e.detail.value].region_id + '.st', {}, function(rep) {
        console.log(rep)
        that.setData({
          district: rep.data.data,
          place: [that.data.province, that.data.city, rep.data.data],
          value: [that.data.one, e.detail.value, 0]
        });
      }, 'GET')
    }
  },
  place: function(e) {
    var _this = this,
      l = _this.data.storeInfo
    l.province = this.data.province[e.detail.value[0]].region_id
    l.city = this.data.city[e.detail.value[1]].region_id
    l.district = this.data.district[e.detail.value[2]].region_id
    this.setData({
      zhi: this.data.province[e.detail.value[0]].region_name + ' ' + this.data.city[e.detail.value[1]].region_name + ' ' + this.data.district[e.detail.value[2]].region_name,
      storeInfo: l
    })
    console.log(_this.data)
  },
  //上传照片方法

  onShowFile(e) {
    var _this = this,
      id = e.currentTarget.id,
      list = _this.data.storeInfo
    wx.chooseImage({
      count: 1,
      success: function(res) {
        console.log(res)
        if (id == 1) {
          _this.setData({
            business: res.tempFilePaths[0]
          })
        } else if (id == 2) {
          _this.setData({
            idcode_positive: res.tempFilePaths[0]
          })
        } else if (id == 3) {
          _this.setData({
            idcode_other: res.tempFilePaths[0]
          })
        } else {
          _this.setData({
            medical: res.tempFilePaths[0]
          })
        }
        wx.uploadFile({
          url: app.link.temporary, //为后端上传代码方法
          filePath: res.tempFilePaths[0],
          name: 'file',
          formData: {
            file: res.tempFilePaths[0]
          },
          success: function(res) {
            let img = JSON.parse(res.data)
            console.log(img.response.file.name)
            if (id == 1) {
              list.business_license = img.response.file.name
            } else if (id == 2) {
              list.idcode_positive = img.response.file.name
            } else if (id == 3) {
              list.idcode_other = img.response.file.name
            } else {
              list.medical = img.response.file.name
            }
            _this.setData({
              storeInfo: list
            })
            console.log(_this.data)
          }
        })
      }
    })

  },
  //下一步提交
  next: function() {
    var _this = this,
      url = app.link.appraisalStore,
      data = _this.data.storeInfo
    _this.setData({
      next: ''
    })
    wx.showModal({
      title: '温馨提示',
      content: '请确认无误后提交，提交后无法更改',
      success(res) {
        console.log(res)
        _this.setData({
          next: 'next'
        })
        if (res.confirm) {
          com.link(url, data, function(rep) {
            console.log(rep)
            com.toast(rep.data.message)
            if (rep.data.state) {
              _this.setData({
                next: 'next'
              })
              setTimeout(() => {
                wx.navigateTo({
                  url: '/pages/contractdetails/contractdetails',
                })
              }, 1500)
            }
          })
        } else {
          console.log('取消')
          _this.setData({
            next: 'next'
          })
        }
      }
    })
  },
  onReady: function() {},
  onShow: function() {
    var _this = this
    wx.getSetting({
      success(res) {
        console.log(res)
        if (res.authSetting["scope.userLocation"] == true) {
          console.log('授权成功')
          var markers = _this.data.markers
          wx.getLocation({
            type: "gcj02",
            success: (res) => {
              console.log('成功：', res)
              markers[0].latitude = res.latitude
              markers[0].longitude = res.longitude
              _this.setData({
                markers: markers
              })
            },
            fail: (res) => {
              _this.setData({
                showLocaltion: true
              })
              console.log('失败：', res)
            },
          })
          _this.setData({
            showLocaltion: false
          })
        } else {
          wx.openSetting({

          })
        }
      }
    })
  },
  onHide: function() {},
  onUnload: function() {},
  onPullDownRefresh: function() {},
  onReachBottom: function() {},
  // onShareAppMessage: function() {}
})